/*
 * Copyright (c) 2008, Yifu Huang
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     * Neither the name of SleekIce nor the
 *       names of its contributors may be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL <copyright holder> BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

package com.sleekice.object;

import java.awt.geom.Point2D;

/* speed and direction for objects */
public class Velocity {

    /* the defaults */
    private static final double DEF_MAX_SPEED = 400;
    private static final double DEF_SPEED = 0;
    private static final double DEF_DIRECTION = 0;
    
    private Acceleration acceleration_;
    private double maxSpeed_;
    private double speed_;
    private double direction_;

    public Velocity() {
        this(DEF_DIRECTION, DEF_SPEED, new Acceleration());
    }

    public Velocity(double direction) {
        this(direction, DEF_SPEED, new Acceleration());
    }

    public Velocity(double direction, double speed) {
        this(direction, speed, new Acceleration());
    }

    public Velocity(double direction, double speed,
        Acceleration acceleration) {
        /* make sure dir is set within range */
        setDirection(direction);
        speed_ = speed;
        maxSpeed_ = DEF_MAX_SPEED;
        acceleration_ = acceleration;
    }
    
    public void addVector(double direction, double speed) {
        double hSpeedInc = Math.cos(Math.toRadians(direction)) * speed;
        double vSpeedInc = Math.sin(Math.toRadians(direction)) * speed;
        double newHSpeed = getHSpeed()+hSpeedInc;
        double newVSpeed = getVSpeed()+vSpeedInc;
        double newSpeed = Math.sqrt(Math.pow(newHSpeed, 2)+Math.pow(newVSpeed,2));
        double newDirection = Math.toDegrees(Math.atan2(newVSpeed,newHSpeed));
        setDirection(newDirection);
        setSpeed(newSpeed);
    }
    
    /**
     * Sets the new pos over a given delta time, returns false if there was
     * no change.
     */
    public void updatePos(Point2D pos, double elapsedTime) {
        /* elapsedTime is in seconds */
        double pixelInc = elapsedTime * speed_;
        double dir = Math.toRadians(direction_);
        double xInc = Math.cos(dir) * pixelInc;
        double yInc = -Math.sin(dir) * pixelInc;
        pos.setLocation(pos.getX() + xInc, pos.getY() + yInc);

        /**
         * Acceleration may act on it even though velocity is still. Prevent
         * the velocity from going over the max speed.
         */ 
        acceleration_.updateVelocity(this, elapsedTime);
        if (speed_ > maxSpeed_) {
            speed_ = maxSpeed_;
        }
    }
    
    public Acceleration getAcceleration() {
        return acceleration_;
    }

    public double getHSpeed() {
        return Math.cos(Math.toRadians(direction_)) * speed_;
    }

    public double getDirection() {
        return direction_;
    }

    public double getSpeed() {
        return speed_;
    }

    public double getVSpeed() {
        return Math.sin(Math.toRadians(direction_)) * speed_;
    }
    
    public void increaseSpeed(double inc) {
        speed_ += inc;
    }
    
    public void setAcceleration(Acceleration acceleration) {
        setAcceleration(acceleration.getDirection(),acceleration.getMagnitude());
    }
    
    public void setAcceleration(double direction, double magnitude) {
        acceleration_.setDirection(direction);
        acceleration_.setMagnitude(magnitude);
    }

    public void setSpeed(double speed) {
        speed_ = speed;
    }

    public void setDirection(double direction) {
        direction_ = direction % 360;
    }
    
    @Override
    public String toString() {
        return "Velocity {Direction = "+direction_+"|Speed = "+speed_+"}";
    }
}